package com.sogoodsoft.web.crm.report;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.sogoodsoft.entity.app.Organization;
import com.sogoodsoft.service.app.OrganizationService;
import com.sogoodsoft.service.crm.report.ReportService;
import com.sogoodsoft.util.InfosysUtils;
import com.sogoodsoft.web.WebConstants;
import com.sogoodsoft.web.common.ListJRDataSource;
import com.sogoodsoft.web.common.Report;

@SuppressWarnings("serial")
public class OrderPerManAction extends ActionSupport {

	private String year;
	private Long org;
	private List<Object> list;
	@Autowired
	private ReportService service;
	@Autowired
	private OrganizationService orgService;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public String execute() throws Exception {
		if(year==null) {
			year = InfosysUtils.getYear(new Date());
			org = new Long(0);
		}
		
		if(WebConstants.mysql){
			 mysql();
		}else{
			oracle();
		}
		
		return SUCCESS;
	}
	
	public void mysql()throws Exception{
		list = new ArrayList<Object>();
		Connection conn =  service.getDao().getSession().connection();
		CallableStatement stat = conn.prepareCall("{call order_per_man(?,?)}");
		logger.debug(year);
		stat.setString(1,year);
		stat.setLong(2, org);
		stat.execute();
		ResultSet rs = stat.getResultSet();
		while(rs.next()){
			
			Object[] obj = {
					rs.getString(1),//姓名
					rs.getLong(5),//销量
					rs.getString(3)//部门
					};
			//{rs.getString(1),rs.getString(2),rs.getString(3)};
			//logger.debug(String.valueOf(obj[1]));
			list.add(obj);
		}
		logger.debug(String.valueOf(list.size()));
		rs.close();
		stat.close();
		conn.close();
	}
	
	
	public void oracle(){
		list = service.query("select maker,oct,node_name from order_per_man where yyyy=? and org=?",year,org);
	}

	@SuppressWarnings("unchecked")
	public String export() throws Exception{
			execute();
			Report report = new Report();	
			report.storeName = "业务员有效订单统计";
			report.ext = Report.EXT.excel;
			String depart = "";
			Organization o = orgService.get(org);
			if(o!=null)depart = o.getName();
			report.para.put("TITLE",year+depart+report.storeName);
			report.para.put("YYYY", year);
			report.para.put("ORG", new BigDecimal(org));
			report.jasper = "OrderPerMan.jasper";
			ListJRDataSource jrs = new ListJRDataSource(list, 2);
			return report.make(jrs);

	}
	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}

	public Long getOrg() {
		return org;
	}

	public void setOrg(Long org) {
		this.org = org;
	}
	
}
